from loguru import logger
import requests

from base.base_login import BaseLogin
from base.env_config import ENV


def hook_response(response, *args, **kwargs):
    logger.info(f'接口响应数据为{response.text}')


class BaseRequest:
    def __init__(self):
        self.session = requests.session()
        self.session.hooks['response'].append(hook_response)


class MerRequests(BaseRequest, BaseLogin):
    def __init__(self):
        super().__init__()
        self.session.headers.update({'authori-zation': self.mer_login(), 'content-type': 'application/json', })

    def get(self, api_name, url, params: dict = None):
        logger.info(f'接口: {api_name} 请求参数为:{ENV.MER_HOST + url},{params}')
        return self.session.get(ENV.MER_HOST + url, params=params, ).json()

    def post(self, api_name, url, data: dict = None):
        logger.info(f'接口: {api_name} 请求参数为:{ENV.MER_HOST + url},{data}')
        return self.session.post(ENV.MER_HOST + url, json=data, ).json()


class PlanRequests(BaseRequest, BaseLogin):
    def __init__(self):
        super().__init__()
        self.session.headers.update({'authori-zation': self.mer_login(), 'content-type': 'application/json', })

    def get(self, api_name, url, params: dict = None):
        logger.info(f'接口: {api_name} 请求参数为:{ENV.MER_HOST + url},{params}')
        return self.session.get(ENV.MER_HOST + url, params=params, ).json()

    def post(self, api_name, url, data: dict = None):
        logger.info(f'接口: {api_name} 请求参数为:{ENV.MER_HOST + url},{data}')
        return self.session.post(ENV.MER_HOST + url, json=data, ).json()


class UserRequests(BaseRequest, BaseLogin):
    def __init__(self):
        super().__init__()
        self.session.headers.update({'authori-zation': self.user_login(), 'content-type': 'application/json', })

    def get(self, api_name, url, params: dict = None):
        logger.info(f'接口: {api_name} 请求参数为:{ENV.MER_HOST + url},{params}')
        return self.session.get(ENV.MER_HOST + url, params=params, ).json()

    def post(self, api_name, url, data: dict = None):
        logger.info(f'接口: {api_name} 请求参数为:{ENV.MER_HOST + url},{data}')
        return self.session.post(ENV.MER_HOST + url, json=data, ).json()
